package com.rong.shop.controller;

import com.rong.shop.controller.ex.*;
import com.rong.shop.service.ex.*;
import com.rong.shop.utils.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
public class BaseController {

    public final static int OK = 200;

    // 文件上传的常量
    // 设置文件大小
    public final static int AVATAR_MAX_SIZE = 10 * 1024 * 1024;
    // 设置文件的类型
    public final static List<String>  AVATAR_TYPE = new ArrayList<>();
    static{
        AVATAR_TYPE.add("image/jpeg");
        AVATAR_TYPE.add("image/png");
        AVATAR_TYPE.add("image/bmp");
        AVATAR_TYPE.add("image/gif");
    }


    // @ExceptionHandler 用来捕获全局的异常，来返回给前端
    @ExceptionHandler({ServiceException.class, FileUploadException.class})
    public JsonResult<Void> hanlderException(Throwable e){

        JsonResult<Void> result = new JsonResult<>();

        if(e instanceof UsernameDuplicatedException){

            result.setState(4000);
            result.setMsg("用户名被占用");

        }else if(e instanceof NotFoundUserException){

            result.setState(4001);
            result.setMsg("没有找到用户！");

        }else if(e instanceof PasswordErrorException){

            result.setState(4002);
            result.setMsg("密码错误！");

        }else if(e instanceof AddressCountLimitException){

            result.setState(4003);
            result.setMsg("收货地址超出限制 20条！");

        }
        else if(e instanceof InsertException){

            result.setState(4000);
            result.setMsg("注册时发生未知错误");

        }else if(e instanceof UpdateException){

            result.setState(5001);
            result.setMsg("更新时的未知异常！");

        }else if (e instanceof FileEmptyException) {

            result.setState(6000);
            result.setMsg("文件为空");

        } else if (e instanceof FileSizeException) {

            result.setState(6001);
            result.setMsg("文件过大");

        } else if (e instanceof FileTypeException) {

            result.setState(6002);
            result.setMsg("文件类型异常");

        } else if (e instanceof FileStateException) {

            result.setState(6003);
            result.setMsg("文件状态异常");

        } else if (e instanceof FileUploadIOException) {

            result.setState(6004);
            result.setMsg("IO异常");

        }else if (e instanceof AddressNotFoundException) {

            result.setState(6005);
            result.setMsg("收货地址没有找到异常");

        }else if (e instanceof AccessDeniedException) {

            result.setState(6006);
            result.setMsg("数据非法访问异常");

        }else if (e instanceof NotFoundProductException) {

            result.setState(6007);
            result.setMsg("没有找到该产品");

        }else if (e instanceof NotFoundCartException) {

            result.setState(6008);
            result.setMsg("没有找到该购物车记录");

        }


        return result;

    }

    /**
     * 子类可以直接调用
     * 封装方法：从session中获取用户ID
     * @return 用户ID
     */
    protected final Integer getUidFromSession(HttpSession session){

        Integer uid = Integer.valueOf(session.getAttribute("uid").toString());
        return uid;

    }

    /**
     * 子类可以直接调用
     * 封装方法：从session中获取 username
     * @return username
     */
    protected final String getUsernameFromSession(HttpSession session){

        String username = session.getAttribute("username").toString();
        return username;

    }


}
